﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class File_File : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string msg = "";
        string flag = "";
        int total = 0;
        long size_total=0;
        int file_index = 0;//已存在文件part  //保存一个分片到磁盘上
        string name = Request["name"];
        total = Convert.ToInt32(Request["total"]);
        int index = Convert.ToInt32(Request["index"]);
        size_total = Convert.ToInt64(Request["size"]);
        var data = Request.InputStream;
        string part = ""; //文件部分
        string file_slice_dir = Server.MapPath("~/Upload/slices");
        string dir = Server.MapPath("~/Upload/test1");
        try
          { //从Request中取参数，注意上传的文件在Requst.Files中
          
            if (Directory.Exists(dir) == false)
            {
                Directory.CreateDirectory(dir);
            }                                         //判断文件是否存在？
            if (Directory.Exists(file_slice_dir) == false)
            {
                Directory.CreateDirectory(file_slice_dir);
            }
            string file = Path.Combine(file_slice_dir, name + "_" + index);  
            string part_all = Path.Combine(dir, name); //文件绝对路径
           
       
             if (index>total) //服务器已经存在大文件

            {
                file_index = total;
                flag = "ok";
            }
             else { 
            if (System.IO.File.Exists(file) == false)
                {
                    file_index = total; //文件上传完成后，赋值100%
                    byte[] bytes = new byte[data.Length];
                    data.Read(bytes, 0, bytes.Length);
                    // 设置当前流的位置为流的开始   
                    data.Seek(0, SeekOrigin.Begin);

                    // 把 byte[] 写入文件   
                    FileStream fs = new FileStream(file, FileMode.Create);
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(bytes);
                    bw.Close();
                    fs.Close();
                }

                //判断文件已上传多少？
                //获取已经上传的文件块数

                Big_file_upload temp_big_file = new Big_file_upload(); //自定义函数

                file_index= temp_big_file.File_part_count(file_slice_dir, name, total);

                //如果已经是最后一个分片，组合
                if (index == total)
                {
                   int loop_num = 0; //累计等待时间
                    while (file_index<total)
                    {
                      System.Threading.Thread.Sleep(1000);//等待一秒，等待所以文件块上传完；
                      file_index= temp_big_file.File_part_count(file_slice_dir, name, total);//继续获取已经上传的文件数
                        loop_num++;
                        if (loop_num == 7200) //60*2分钟停止等待
                        {
                          break;
                        }
                    }
                        try
                        {
                            file = Path.Combine(dir, name);
                            var fs = new FileStream(file, FileMode.Create);
                            for (int i = 1; i <= total; ++i)
                            {
                                part = Path.Combine(file_slice_dir, name + "_" + i);
                                var bytes = System.IO.File.ReadAllBytes(part);
                                fs.Write(bytes, 0, bytes.Length);
                                bytes = null;
                                System.IO.File.Delete(part);
                            }
                            fs.Close();
                            flag = "ok";
                        }
                        catch (Exception)
                        {
                            flag = "error";        
                        }
                }
              
            }


        }
        catch (Exception)
        {
            flag = "error";
            //返回是否成功，是否是最后一片，文件是否存在，文件是否已经部分上传，上传到了多少片

            //出错删除所有的分片 20180129
            for (int i = 1; i <= total; ++i)
            {
                part = Path.Combine(file_slice_dir, name + "_" + i);
             System.IO.File.Delete(part);
            }
            //
        }
    
        msg = flag + "|" + file_index + "|" + total + "|" + size_total;
        Response.Write(msg);
    }
   
}